home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4395 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.1 KB  |  210 lines

  1. Path: ix.netcom.com!netnews
  2. From: jdmorris@ix.netcom.com (Jason D. Morris)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Why C++ sucks++
  5. Date: Tue, 30 Jan 1996 04:22:50 GMT
  6. Organization: Netcom
  7. Message-ID: <4ek6e8$s76@reader2.ix.netcom.com>
  8. References: <1996Jan29.223357.1@aspen>
  9. NNTP-Posting-Host: ix-pon-mi2-03.ix.netcom.com
  10. X-NETCOM-Date: Mon Jan 29  8:22:35 PM PST 1996
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. ferriom@aspen wrote:
  14.  
  15.  
  16.  
  17. >*** HIGH SCHOOL ***
  18.  
  19. >100  PRINT "Hello World."
  20. >999  END
  21.  
  22.  
  23.  
  24. >*** COLLEGE FRESHMAN ***
  25.  
  26. >       WRITE (6, 100)
  27. >  100  FORMAT (1X, 'Hello World.')
  28. >       END
  29.  
  30.  
  31.  
  32. >*** ADVANCED COLLEGE ***
  33.  
  34. >PROGRAM P1(INPUT, OUTPUT);
  35.  
  36. >BEGIN
  37. >WRITELN(OUTPUT, 'Hello World.');
  38. >END.
  39.  
  40.  
  41.  
  42. >*** RECENT COLLEGE GRADUATE ***
  43.  
  44. >       IDENTIFICATION DIVISION.
  45. >       PROGRAM-ID.
  46. >           P1.
  47.  
  48. >       ENVIRONMENT DIVISION.
  49.  
  50. >       DATA DIVISION.
  51. >       WORKING-STORAGE SECTION.
  52. >       01 MESSAGE-RECORD                     PICTURE X(12).
  53.  
  54. >       PROCEDURE DIVISION.
  55. >       PROGRAM-BEGIN.
  56. >       FIRST-PARAGRAPH.
  57. >           MOVE 'Hello World.' TO MESSAGE-RECORD.
  58. >           DISPLAY MESSAGE-RECORD.
  59. >       PROGRAM-DONE.
  60. >           STOP RUN.
  61.  
  62.  
  63.  
  64. >*** SEASONED PROFESSIONAL ***
  65.  
  66. >#include <iostream.h>
  67. >#include <string.h>
  68.  
  69.  
  70. >class String
  71. >{
  72. > public:
  73. >  String();
  74. >  String(const char *const);
  75. >  String(const String &);
  76. >  ~String();
  77. >  char & operator[](unsigned short offset);
  78. >  char operator[](unsigned short offset) const;
  79. >  String operator+(const String&);
  80. >  void operator+=(const String&);
  81. >  String & operator= (const String &);
  82. >  unsigned short GetLen()const {return itsLen; }
  83. >  const char * GetString() const {return itsString; }
  84. > private:
  85. >  String (unsigned short);
  86. >  char * itsString;
  87. >  unsigned short itsLen;
  88. >};
  89.  
  90.  
  91. >String::String()
  92. >{
  93. > itsString = new char[1];
  94. > itsString[0] = '\0';
  95. > itsLen = 0;
  96. >}
  97.  
  98. >String::String(unsigned short len)
  99. >{
  100. > itsString = new char[len + 1];
  101. > for (unsigned short i = 0;  i <= len;  i++)
  102. >  itsString[i] = '\0';
  103. > itsLen = len;
  104. >}
  105.  
  106. >String::String(const char * const cString)
  107. >{
  108. > itsLen = strlen(cString);
  109. > itsString = new char[itsLen + 1];
  110. > for (unsigned short i = 0;  i < itsLen;  i++)
  111. >  itsString[i] = cString[i];
  112. > itsString[itsLen] = '\0';
  113. >}
  114.  
  115. >String::String(const String & rhs)
  116. >{
  117. > itsLen = rhs.GetLen();
  118. > itsString = new char[itsLen + 1];
  119. > for (unsigned short i = 0;  i < itsLen;  i++)
  120. >  itsString[i] = rhs[i];
  121. > itsString[itsLen] = '\0';
  122. >}
  123.  
  124. >String::~String()
  125. >{
  126. > delete [] itsString;
  127. > itsLen = 0;
  128. >}
  129.  
  130. >String& String::operator=(const String & rhs)
  131. >{
  132. > if (this == &rhs)
  133. >  return *this;
  134. > delete [] itsString;
  135. > itsLen = rhs.GetLen();
  136. > itsString = new char[itsLen + 1];
  137. > for (unsigned short i = 0;  i < itsLen;  i++)
  138. >  itsString[i] = rhs[i];
  139. > itsString[itsLen] = '\0';
  140. > return *this;
  141. >}
  142.  
  143. >char & String::operator[](unsigned short offset)
  144. >{
  145. > if (offset > itsLen)
  146. >  return itsString[itsLen - 1];
  147. > else
  148. > return itsString[offset];
  149. >}
  150.  
  151. >char String::operator[](unsigned short offset) const
  152. >{
  153. > if (offset > itsLen)
  154. >  return itsString[itsLen - 1];
  155. > else
  156. >  return itsString[offset];
  157. >}
  158.  
  159. >String String::operator+(const String& rhs)
  160. >{
  161. > unsigned short totalLen = itsLen + rhs.GetLen();
  162. > String temp(totalLen);
  163. > for (unsigned short i = 0;  i < itsLen;  i++)
  164. >  temp[i] = itsString[i];
  165. > for (unsigned short j = 0;  j < rhs.GetLen();  j++, i++)
  166. >  temp[i] = rhs[j];
  167. > temp[totalLen] = '\0';
  168. > return temp;
  169. >}
  170.  
  171. >void String::operator+=(const String& rhs)
  172. >{
  173. > unsigned short rhsLen = rhs.GetLen();
  174. > unsigned short totalLen = itsLen + rhsLen;
  175. > String temp(totalLen);
  176. > for (unsigned short i = 0;  i < itsLen;  i++)
  177. >  temp[i] = itsString[i];
  178. > for (unsigned short j = 0;  j < rhs.GetLen();  j++,  i++)
  179. >  temp[i] = rhs[i - itsLen];
  180. > temp[totalLen] = '\0';
  181. > *this = temp; 
  182. >}
  183.  
  184.  
  185. >main()
  186. >{
  187. > String s1("Hello");
  188. > String s2(" ");
  189. > String s3("World.");
  190. > String s4 = s1 + s2 + s3;
  191. > cout << s4.GetString() << endl;
  192. >}
  193. Ok, I'll take the bait...
  194.  
  195. *** SEASONED PROFESSIONAL WHO HAS *READ* THE DRAFT STANDARD ***
  196. #include <iostream>
  197. #include <string>
  198.  
  199. main()
  200. {
  201.   string s1("Hello");
  202.   string s2(" ");
  203.   string s3("World.");
  204.   string s4 = s1 + s2 + s3;
  205.   cin >> s4;  
  206.   cout << s4 << endl;
  207. }
  208.  
  209.  
  210.